package com.mgg.goods.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mgg.common.util.BeanUtils;
import com.mgg.common.util.ResultVOUtil;
import com.mgg.goods.entity.GoodsDO;
import com.mgg.goods.service.BaseGoodsService;
import com.mgg.goods.service.store.StoreService;
import com.mgg.goods.vo.GoodsSimpleVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("store")
public class StoreController {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private StoreService storeService;
	
	@Autowired
	private BaseGoodsService baseGoodsService;
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	/**
	 * 门店列表
	 * @param storeName
	 * @param lng
	 * @param lat
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value = "/list")
	public Object getStoreList(@RequestHeader(value = "user_id", required = false) String userId,
	        @RequestParam(value = "store_name", required = false) String storeName,
			@RequestParam(required = false, defaultValue="113.372528") Double lng, 
			@RequestParam(required = false, defaultValue="22.984118") Double lat,
			@PageableDefault(page = 1, size = 10) Pageable page) {
		return ResultVOUtil.success(storeService.listStore(lng,lat,storeName,page));
	}

    /**
     * 门店统计
     * @return
     * @throws Exception
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value = "/count")
    public Object getStoreCount() {
        return ResultVOUtil.success(storeService.countStoreNum());
    }

    /**
     * 搜索门店
     * @param storeName
     * @param page
     * @return
     * @throws Exception
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value = "/search")
    public Object searchStoreList(@RequestHeader(value = "user_id", required = false) String userId, @RequestParam("name") String storeName, @PageableDefault(page = 1, size = 10) Pageable page) {
        List<Map<String,Object>> storeList = storeService.listStoreByName(storeName,page);
        return ResultVOUtil.success(storeList);
    }

    /**
     * 门店推荐商品列表
     * @param storeCode
     * @param sort
     * @param page
     * @return
     * @throws Exception
     */
    @GetMapping(path="/product/list/recd")
    public Object listStoreRecdProduct(@RequestParam(value="store_code") String storeCode, @RequestParam(required = false) String sort, @PageableDefault(page = 1, size = 10) Pageable page) {
        List<GoodsDO> goodsDOList = baseGoodsService.listGoodsByStoreCode(storeCode, sort,"1", true, page);
        List<GoodsSimpleVO> goodsList = initGoodsList(goodsDOList);
        return ResultVOUtil.success(goodsList);
    }

    /**
     * 门店商品列表
     * @param storeCode
     * @param sort
     * @param page
     * @return
     * @throws Exception
     */
    @GetMapping(path="/goodslist")
    public Object storeGoods(@RequestParam(value="store_code") String storeCode, @RequestParam(required = false) String sort, @PageableDefault(page = 1, size = 10) Pageable page) {
        List<GoodsDO> goodsDOList = baseGoodsService.listGoodsByStoreCode(storeCode, sort, "1", null, page);
        List<GoodsSimpleVO> goodsList = initGoodsList(goodsDOList);
        return ResultVOUtil.success(goodsList);
    }

    private List<GoodsSimpleVO> initGoodsList(List<GoodsDO> goodsDOList) {
        List<GoodsSimpleVO> goodsList = new ArrayList<>(goodsDOList.size());
        for (GoodsDO goodsDO : goodsDOList) {
            GoodsSimpleVO goodsSimpleVO = new GoodsSimpleVO();
            BeanUtils.copyProperties(goodsDO,goodsSimpleVO);
            String sharePic = goodsDO.getMainPic().split(",")[0];
            goodsSimpleVO.setSharePic(sharePic);
            goodsList.add(goodsSimpleVO);
        }
        return goodsList;
    }

    /**
     * 根据商品获取门店列表
     * @param goodsId
     * @param page
     * @return
     * @throws Exception
     * @throws UnsupportedEncodingException
     */
    @GetMapping(value = "/list/bygoods")
    public Object getStoreListByGoods(@RequestParam String goodsId, @RequestParam Double lng, @RequestParam Double lat,
                                      @RequestParam(value = "store_name", required = false) String storeName, @PageableDefault(page = 1, size = 10) Pageable page) {
        return ResultVOUtil.success(storeService.listStoreByGoodsId(goodsId, lng, lat, storeName, page));
    }






}
